package com.stlm2.controller;

import com.google.gson.Gson;
import com.stlm2.core.common.md5.MD5;
import com.stlm2.core.redis.RedisClient;
import com.stlm2.dataSource.DBContextHolder;
import com.stlm2.dems.entity.Address;

import com.stlm2.dems.service.AddressService;
import com.stlm2.dems.dao.ConfigDao;
import com.stlm2.dems.entity.Config;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.sns.entity.Platform;
import com.stlm2.dems.sns.service.SmsService;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.user.entity.*;
import com.stlm2.dems.user.service.AccountService;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.Memcached;
import com.stlm2.util.RegularMatchUtils;
import net.sf.json.JSONArray;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by songfangyuan on 2017/2/27.
 */
@Controller

@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UserDao userDao;
    @Resource
    private ConfigDao configDao;
    @Resource
    private Memcached memcached;
    @Resource
    private SmsService smsService;
    @Resource
    private AccountService accountService;
    @Resource
    private AddressService addressService;

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

/*
* sfy
* test
* 动态切换数据源
* */

    @ResponseBody
    @RequestMapping(value="/sfytest.json",produces="application/json;charset=UTF-8")
    public String sfytest(String companyCode,HttpServletRequest request,ModelMap model) {

         User user = userService.getById(12);
        System.out.println(user.getName());
        return  null;
    }

/*
* sfy
* test
* 动态切换数据源
* */

    @ResponseBody
    @RequestMapping(value="/sfytest2.json",produces="application/json;charset=UTF-8")
    public String sfytest2(String companyCode,HttpServletRequest request,ModelMap model) {
         /*
         * sfy 在相应的地方增加对companyCode参数的判断。
         *
         * */

        User user = userService.getById(12);
        System.out.println("获取用户id为12的账号"+user.getName()+"，公司编号为："+companyCode);
        return  null;
    }
    /*
* wap获取用户key
* name
* key
*
* */
    @ResponseBody
     @RequestMapping(value="/userLoginKey.json",produces="application/json;charset=UTF-8")
    public String userLoginKey(String companyCode,String name,HttpServletRequest request,ModelMap model)  {

       String data;
       try {
        data=   userService.getUserKey(name);
       }catch (Exception e){
           e.printStackTrace();
           return CodeContent.getModelByCode(0,"网络超时或用户账号问题",model);
       }
       return data;
    }

    /*
    * wap登录接口
    * userName passWord
    *
    * */
    @ResponseBody
    @RequestMapping(value="/userLogin.json",produces="application/json;charset=UTF-8")
    public String userLogin(String companyCode,ModelMap model, String userName, String passWord,HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        Map<String,Object> map = new HashMap<String, Object>();
        if (org.apache.commons.lang.StringUtils.isBlank(userName) || org.apache.commons.lang.StringUtils.isBlank(passWord)) {

            return CodeContent.getModelByCode(0,"用户名密码不能为空", model);
        }
        //判断是否是合法的用户  begin
        User user = userService.getByName(userName.trim());
        if (user == null) {
            return CodeContent.getModelByCode(0,"用户名不正确", model);
        }

        String password2 = user.getPassword();
        //判断密码是否正确
        if (!passWord.equals(password2)) {
            return CodeContent.getModelByCode(0,"密码错误", model);
        }
        //判断是否是合法的用户  end
        if (UserState.Invalid.equals(user.getUserState())) {
            return CodeContent.getModelByCode(0,"用户状态无效", model);
        }
        if(UserType.Normal.equals(user.getUserType())||UserType.Merchant.equals(user.getUserType())||UserType.ChildMerchant.equals(user.getUserType())){
            //用户为前台账户
        }else {
            return CodeContent.getModelByCode(0,"无权限登陆",null);
        }
        //合法用户的操作：：：：：
        String	token = MD5.encryptMD5(user.getId() + user.getPassword() + new Date().toString());
        model.put("token", token);
        Gson gson = new Gson();
        model.put("realName",user.getRealName());
        model.put("userTypeValue",user.getUserTypeValue());
        model.put("userType",user.getUserType()!=null?user.getUserType().toString():"");
        memcached.set(token, 15*24*60*60, gson.toJson(user).toString()); //将对象和其token值存放在缓存中15天
        userService.userTokenLoginCheck(token,user.getId());
        model.put("userId",user.getId());
        return CodeContent.getModelByCode(1,"success", model);



    }

    @ResponseBody
    @RequestMapping(value="/appVersion.json",produces="application/json;charset=UTF-8")
    public  String appVersionCode(String companyCode){
        ModelMap model=new ModelMap();
        Config config=new Config();
        config.setConfigName("IOS_VERSION_CODE");
        config=configDao.selectOneFromRedis(config);
        if(config!=null&&config.getConfigValue()!=null){
            model.put("iosVersionCode",config.getConfigValue());
            model.put("iosMemo",config.getMemo()!=null?config.getMemo():"");
        }else {
            Config configIos=configDao.getById(3);
            if(configIos!=null){
                List<String>  list = new ArrayList<String>();
                list.add("configName");
                configDao.addIntoRedis(configIos,list);
                model.put("iosVersionCode",configIos.getConfigValue());
                model.put("iosMemo",configIos.getMemo()!=null?configIos.getMemo():"");
            }else {
                return CodeContent.getModelByCode(0,"未找到APP_IOS版本号",model);
            }
        }

        Config config1=new Config();
        config1.setConfigName("ANDROID_VERSION_CODE");
        config1=configDao.selectOneFromRedis(config1);
        if(config1!=null&&config1.getConfigValue()!=null){
            model.put("androidVersionCode",config1.getConfigValue());
            model.put("androidMemo",config1.getMemo()!=null?config1.getMemo():"");
        }else {
            Config configAndroid=configDao.getById(4);
            if(configAndroid!=null){
                List<String>  list = new ArrayList<String>();
                list.add("configName");
                configDao.addIntoRedis(configAndroid,list);
                model.put("androidVersionCode",configAndroid.getConfigValue());
                model.put("androidMemo",configAndroid.getMemo()!=null?configAndroid.getMemo():"");
            }else {
                return CodeContent.getModelByCode(0,"未找到APP安卓版本号",model);
            }
        }
        return CodeContent.getModelByCode(1,"",model);

    }


    @ResponseBody
    @RequestMapping("/search.json")
    public String searchUser(String companyCode){
       User user=  userService.findEntityById(1);
        String mobileCode = String.valueOf(Math.random()).substring(2, 8);
        String smsString = smsService.sendCode(Platform.Ios, null, "13353613565", mobileCode);

        return JSONArray.fromObject(user).toString();
    }

    /**
     *  短信验证登陆
     */
    @ResponseBody
    @RequestMapping(value="/userLogin2.json",produces="application/json;charset=UTF-8")
    public String userLogin2(String companyCode,ModelMap model, String userName,HttpServletResponse response,String mobile_code,String platform,String upperUserId,String isdzp){
        response.setCharacterEncoding("UTF-8");

        // ffl    判断手机号、验证码、平台是否为空

        if (org.apache.commons.lang.StringUtils.isBlank(userName)) {
            return CodeContent.getModelByCode(0,"请输入手机号", model);
        }
        if (org.apache.commons.lang.StringUtils.isBlank(mobile_code)) {

            return CodeContent.getModelByCode(0,"请输入验证码", model);
        }
        if(org.apache.commons.lang.StringUtils.isBlank(platform)){
            return CodeContent.getModelByCode(0,"请输入平台", model);
        }


        String result = "";

        if("ios".equals(platform)){
            result = smsService.checkCode(Platform.Ios, userName, mobile_code,memcached.getCache());

        }else if("android".equals(platform)){

            result = smsService.checkCode(Platform.Android, userName, mobile_code,memcached.getCache());
        }
        else if("wap".equals(platform)){
            result = smsService.checkCode(Platform.Wap, userName, mobile_code,memcached.getCache());

        }else if("web".equals(platform)){
            result = smsService.checkCode(Platform.Web, userName, mobile_code,memcached.getCache());
        }
        if(!"success".equals(result)){
            return CodeContent.getModelByCode(0,result, model);
        }

        boolean ifdzp=false;
        if(isdzp!=null && "true".equals(isdzp)){
            ifdzp=true;
        }
        int companyC=1;
        if(NumberUtils.isDigits(companyCode)){
           companyC=NumberUtils.toInt(companyCode);
        }

        //判断是否是合法的用户  begin
        User user = userService.getByName(userName.trim());
        if (user == null) {
          user=userService.registerDo(userName.trim(),upperUserId,ifdzp,companyC);
        }
        //判断是否是合法的用户  end
        if (UserState.Invalid.equals(user.getUserState())) {
            return CodeContent.getModelByCode(0,"手机号暂不可用", model);
        }
        if(UserType.Normal.equals(user.getUserType())||UserType.Merchant.equals(user.getUserType())||UserType.ChildMerchant.equals(user.getUserType())){
            //用户为前台账户
        }else {
            return CodeContent.getModelByCode(0,"无权限登陆",null);
        }
        //合法用户的操作：：：：：
        String	token = MD5.encryptMD5(user.getId() + user.getPassword() + new Date().toString());
        model.put("token", token);
        Gson gson = new Gson();
        model.put("realName",user.getRealName());
        model.put("userTypeValue",user.getUserTypeValue());
        model.put("userType",user.getUserType()!=null?user.getUserType().toString():"");
        memcached.set(token, 15*24*60*60, gson.toJson(user).toString()); //将对象和其token值存放在缓存中15天

        //用户最多有三个有效token
        userService.userTokenLoginCheck(token,user.getId());
        model.put("userId",user.getId());
        return CodeContent.getModelByCode(1,"sucess", model);



    }




    @ResponseBody
    @RequestMapping(value="/sendMobileCode_v2.json",produces="application/json;charset=UTF-8")
    public String sendMobileCode(String companyCode,ModelMap model, HttpServletResponse response, String mobile, String platform,String validateCode) {
        response.setCharacterEncoding("utf-8");
        validateCode=StringUtils.trimAllWhitespace(validateCode);
        String smsString = "";
        if (StringUtils.isEmpty(mobile)) {
            return  CodeContent.getModelByCode(0, "请输入手机号", model);
        }
        User user=userService.getByName(mobile);
        if(user!=null){
            return  CodeContent.getModelByCode(-1,"", null);
        }
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = RedisClient.getPool();
            jedis = pool.getResource();
            StringBuffer key2=new StringBuffer();
            key2.append("ValidateCode:");
            key2.append(mobile + ":");
            key2.append("true:");
            key2.append(validateCode.toLowerCase());
            boolean boo =jedis.exists(key2.toString());
            if(!boo){
                return CodeContent.getModelByCode(0,"图形验证码验证失败", model);
            }
            //生成手机验证码
            String mobileCode = String.valueOf(Math.random()).substring(2, 8);
            System.out.println("mobileCode:"+mobileCode);

            if ("ios".equals(platform)) {
                smsString = smsService.sendCode(Platform.Ios, null, mobile, mobileCode);
            } else if ("android".equals(platform)) {
                smsString = smsService.sendCode(Platform.Android, null, mobile, mobileCode);
            } else {
                smsString = smsService.sendCode(Platform.Wap, null, mobile, mobileCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CodeContent.getModelByCode(0,"发送验证码失败", model);
        }finally {
            pool.returnResource(jedis);
        }
        if("success".equals(smsString)){
            return CodeContent.getModelByCode(1,"sucess", model);
        }else{
            return CodeContent.getModelByCode(0,smsString, model);
        }
    }

    @ResponseBody
    @RequestMapping(value="/checkMobileCode.json",produces="application/json;charset=UTF-8")
    public String appCheckMobileCode(String companyCode,ModelMap model,HttpServletRequest request,HttpServletResponse response, String mobile, String mobile_code, String k,String token,String platform) {
        response.setCharacterEncoding("utf-8");

        if (org.apache.commons.lang.StringUtils.isBlank(k)) {// ffl    判断手机号、验证码、平台是否为空

            if (org.apache.commons.lang.StringUtils.isBlank(mobile)) {
                return CodeContent.getModelByCode(0,"请输入手机号", model);
            }
            if (org.apache.commons.lang.StringUtils.isBlank(mobile_code)) {

                return CodeContent.getModelByCode(0,"请输入验证码", model);
            }
            if(org.apache.commons.lang.StringUtils.isBlank(platform)){
                return CodeContent.getModelByCode(0,"请输入平台", model);
            }
        }  else if (org.apache.commons.lang.StringUtils.isNotBlank(k) && org.apache.commons.lang.StringUtils.isNotBlank(token)) {

            User user = userService.getByToken(token);
            if (user != null) {
                mobile = user.getName();
            } else {
                return CodeContent.getModelByCode(0,"您的登录已经过期，请重新登录", model);
            }
        }
        String result = "";

        if("ios".equals(platform)){
            result = smsService.checkCode(Platform.Ios, mobile, mobile_code,memcached.getCache());

        }else if("android".equals(platform)){

            result = smsService.checkCode(Platform.Android, mobile, mobile_code,memcached.getCache());
        }
        else if("wap".equals(platform)){
            result = smsService.checkCode(Platform.Wap, mobile, mobile_code,memcached.getCache());

        }else if("web".equals(platform)){
            result = smsService.checkCode(Platform.Web, mobile, mobile_code,memcached.getCache());
        }
        if("success".equals(result)){
            return CodeContent.getModelByCode(1,"success", model);
        } else{
            return CodeContent.getModelByCode(0,result, model);
        }

    }

    /**
     * 检验注册邀请码和短信验证码
     * ffl
     * 2017-08-31 11:07:07
     * @param model
     * @param request
     * @param response
     * @param mobile
     * @param mobile_code
     * @param id
     * @param platform
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/checkMobileCode_v2.json",produces="application/json;charset=UTF-8")
       public String appCheckMobileCode_v2(String companyCode,ModelMap model,HttpServletRequest request,HttpServletResponse response, String mobile, String mobile_code, String id,String platform) {
        response.setCharacterEncoding("utf-8");

        if (org.apache.commons.lang.StringUtils.isBlank(mobile)) {
            return CodeContent.getModelByCode(0,"请输入手机号", null);
        }
        if (org.apache.commons.lang.StringUtils.isBlank(mobile_code)) {

            return CodeContent.getModelByCode(0,"请输入验证码", null);
        }
        if(org.apache.commons.lang.StringUtils.isBlank(platform)){
            return CodeContent.getModelByCode(0,"请输入平台", null);
        }
        if(com.stlm2.util.StringUtils.isNullOrEmpty(id)){
            id="1";
        }
        if(!NumberUtils.isDigits(id)){
            return CodeContent.getModelByCode(0,"请输正确的格式的邀请码",null);
        }

        String result = "";

        if("ios".equals(platform)){
            result = smsService.checkCode(Platform.Ios, mobile, mobile_code,memcached.getCache());

        }else if("android".equals(platform)){

            result = smsService.checkCode(Platform.Android, mobile, mobile_code,memcached.getCache());
        }
        else if("wap".equals(platform)){
            result = smsService.checkCode(Platform.Wap, mobile, mobile_code,memcached.getCache());

        }else if("web".equals(platform)){
            result = smsService.checkCode(Platform.Web, mobile, mobile_code,memcached.getCache());
        }
        if("success".equals(result)){
            Map<String,Object>map=userService.checkUpperUser(NumberUtils.toInt(id),mobile);
            if(map.get("code").equals(-1)){
                return CodeContent.getModelByCode(-1,map.get("msg").toString(), null);
            }else if(map.get("code").equals(0)) {
                return CodeContent.getModelByCode(0,map.get("msg").toString(), null);
            }else {
                model.put("upperPhone",map.get("upperPhone"));
                model.put("upperName",map.get("upperName"));
                model.put("userKey",map.get("userKey"));
                return CodeContent.getModelByCode(1,"success", model);
            }

        } else{
            return CodeContent.getModelByCode(0,result, null);
        }

    }

    /**
     *  新用户通过邀请码注册登陆
     * ffl
     * 2017-09-01 10:57:04
     * @param model
     * @param request
     * @param response
     * @param mobile
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/register.json",produces="application/json;charset=UTF-8")
    public String register(String companyCode,ModelMap model,HttpServletRequest request,HttpServletResponse response, String mobile, Integer id,String  isdzp) {
        response.setCharacterEncoding("utf-8");
        boolean ifdzp=false;
        if(isdzp!=null && "true".equals(isdzp)){
            ifdzp=true;
        }
        return userService.register(id,mobile,ifdzp);
    }


    @ResponseBody
    @RequestMapping(value="/checkTwoMobileCode.json",produces="application/json;charset=UTF-8")
    public String appCheckMobileCode2(String companyCode,ModelMap model,HttpServletRequest request,HttpServletResponse response, String mobileOld, String code1, String mobileNew,String code2, String token,String platform) {
        response.setCharacterEncoding("utf-8");
         if (org.apache.commons.lang.StringUtils.isBlank(token)) {
           return CodeContent.getModelByCode(-1,"",model);
        }
        User user= userService.getByToken(token);
        if (user != null) {
            mobileOld = user.getName();
        } else {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        String result = "";

        if("ios".equals(platform)){
            result = smsService.checkCode(Platform.Ios, mobileOld, code1,memcached.getCache());

        }else if("android".equals(platform)){

            result = smsService.checkCode(Platform.Android, mobileOld, code1,memcached.getCache());
        }
        else if("wap".equals(platform)){
            result = smsService.checkCode(Platform.Wap, mobileOld, code1,memcached.getCache());

        }else if("web".equals(platform)){
            result = smsService.checkCode(Platform.Web, mobileOld, code1,memcached.getCache());
        }
        if("success".equals(result)){
           String result2="";
            if("ios".equals(platform)){
                result2 = smsService.checkCodeTwice(Platform.Ios, mobileNew, code2,mobileOld,user,token);

            }else if("android".equals(platform)){

                result2 = smsService.checkCodeTwice(Platform.Android, mobileNew, code2,mobileOld,user,token);
            }
            else if("wap".equals(platform)){
                result2 = smsService.checkCodeTwice(Platform.Wap, mobileNew, code2,mobileOld,user,token);

            }else if("web".equals(platform)){
                result2 = smsService.checkCodeTwice(Platform.Web, mobileNew, code2,mobileOld,user,token);
            }
            if(result2.equals("success")){
              return CodeContent.getModelByCode(1,result2,model);
            }else {
                return CodeContent.getModelByCode(0,"新手机号验证码错误",model);
            }

        } else{
            return CodeContent.getModelByCode(0,"旧手机号验证码错误", model);
        }

    }

    //修改登陆密码和交易密码
    @ResponseBody
    @RequestMapping(value="/updatePassword.json",produces="application/json;charset=UTF-8")
    public String updatePassword(String companyCode,ModelMap model,String token, String password,String payPassword,HttpServletResponse response,String mobile_code,String platform){
        response.setCharacterEncoding("UTF-8");

        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }

        if (org.apache.commons.lang.StringUtils.isBlank(password)&&org.apache.commons.lang.StringUtils.isBlank(payPassword) ) {

            return CodeContent.getModelByCode(0,"密码不能为空", model);
        }

        if (org.apache.commons.lang.StringUtils.isBlank(mobile_code)) {

            return CodeContent.getModelByCode(0,"请输入验证码", model);
        }
        if(org.apache.commons.lang.StringUtils.isBlank(platform)){
            return CodeContent.getModelByCode(0,"请输入平台", model);
        }


        String result = "";

        if("ios".equals(platform)){
            result = smsService.checkCode(Platform.Ios, user.getName(), mobile_code,memcached.getCache());

        }else if("android".equals(platform)){

            result = smsService.checkCode(Platform.Android, user.getName(), mobile_code,memcached.getCache());
        }
        else if("wap".equals(platform)){
            result = smsService.checkCode(Platform.Wap, user.getName(), mobile_code,memcached.getCache());

        }else if("web".equals(platform)){
            result = smsService.checkCode(Platform.Web, user.getName(), mobile_code,memcached.getCache());
        }
        if(!"success".equals(result)){
            return CodeContent.getModelByCode(0,result, model);
        }


        //修改用户密码
        if(!org.apache.commons.lang.StringUtils.isBlank(password)){
            user=userService.getById(user.getId());
            user.setPassword(password);
            userService.updateUser(user,token);
        }
        //修改交易密码
        if(!org.apache.commons.lang.StringUtils.isBlank(payPassword)){
            Account account =accountService.getByUserId(user.getId());
            account.setPassword(payPassword);
            accountService.updateEntity(account);
        }

        return CodeContent.getModelByCode(1,"sucess", model);

    }
    @ResponseBody
    @RequestMapping(value="/findPassword.json",produces="application/json;charset=UTF-8")
    public String Password(String companyCode,ModelMap model,String phone, String password,HttpServletResponse response,String mobile_code,String platform){
        response.setCharacterEncoding("UTF-8");

        // ffl    判断手机号、验证码、平台是否为空

        if (org.apache.commons.lang.StringUtils.isBlank(phone)) {
            return CodeContent.getModelByCode(0,"请输入手机号", model);
        }
        if (org.apache.commons.lang.StringUtils.isBlank(mobile_code)) {

            return CodeContent.getModelByCode(0,"请输入验证码", model);
        }
        if(org.apache.commons.lang.StringUtils.isBlank(platform)){
            return CodeContent.getModelByCode(0,"请输入平台", model);
        }


        String result = "";

        if("ios".equals(platform)){
            result = smsService.checkCode(Platform.Ios,phone, mobile_code,memcached.getCache());

        }else if("android".equals(platform)){

            result = smsService.checkCode(Platform.Android, phone, mobile_code,memcached.getCache());
        }
        else if("wap".equals(platform)){
            result = smsService.checkCode(Platform.Wap, phone, mobile_code,memcached.getCache());

        }else if("web".equals(platform)){
            result = smsService.checkCode(Platform.Web, phone, mobile_code,memcached.getCache());
        }
        if(!"success".equals(result)){
            return CodeContent.getModelByCode(0,result, model);
        }


        if (StringUtils.isEmpty(phone)) {
            return CodeContent.getModelByCode(0,"请提供手机号", model);
        }

        if (org.apache.commons.lang.StringUtils.isBlank(password) ) {

            return CodeContent.getModelByCode(0,"密码不能为空", model);
        }
        User user=userService.getByName(phone);
        if(user==null){
            return CodeContent.getModelByCode(0,"用户不存在，请检查手机号", model);
        }
        //修改用户密码
        user.setPassword(password);
        userService.updateEntity(user);

        return CodeContent.getModelByCode(1,"sucess", model);

    }

    @ResponseBody
    @RequestMapping(value="/updateRealName.json",produces="application/json;charset=UTF-8")
    public String updateRealName(String companyCode,ModelMap model,String token, String realName,HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");

//        try {
//            realName = new String (realName.getBytes("iso8859-1"), "UTF-8");
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }

        if (org.apache.commons.lang.StringUtils.isBlank(realName) ) {

            return CodeContent.getModelByCode(0,"真实姓名不能为空", model);
        }
        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        user=userService.getById(user.getId());
        //修改用户姓名
        user.setRealName(realName.trim());
        userService.updateUser(user,token);
        return CodeContent.getModelByCode(1,"sucess", model);

    }
    @ResponseBody
    @RequestMapping(value="/updateSex.json",produces="application/json;charset=UTF-8")
    public String updateSex(String companyCode,ModelMap model,String token, String sexValue,HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");

        Sex sex = Sex.getByIntValue(NumberUtils.toInt(sexValue));
        if (sex==null ) {
            return CodeContent.getModelByCode(0,"请选择性别", model);
        }
        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        user=userService.getById(user.getId());
        //修改用户性别
        user.setSex(sex);
        userService.updateUser(user,token);

        return CodeContent.getModelByCode(1,"sucess", model);

    }
    @ResponseBody
    @RequestMapping(value="/updateEmail.json",produces="application/json;charset=UTF-8")
    public String updateEmail(String companyCode,ModelMap model,String token, String email,HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");

        if (org.apache.commons.lang.StringUtils.isBlank(email)) {
            return CodeContent.getModelByCode(0,"邮箱不能为空", model);
        } else if (!RegularMatchUtils.isEmail(email.trim())) {
            return CodeContent.getModelByCode(0,"请输入有效的邮箱", model);
        } else if(userService.isEmailExist(email)){
            return CodeContent.getModelByCode(0,"邮箱已经存在", model);

        } else if (email.trim().length() > 50) {
            return CodeContent.getModelByCode(0,"邮箱长度不能大于50", model);
        }

        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        user=userService.getById(user.getId());
        //修改用户邮箱
        user.setEmail(email);
        userService.updateUser(user,token);
        return CodeContent.getModelByCode(1,"sucess", model);

    }

    @ResponseBody
    @RequestMapping(value="/updateWeixin.json",produces="application/json;charset=UTF-8")
    public String updateWeixin(String companyCode,ModelMap model,String token, String weixin,HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");

        if (org.apache.commons.lang.StringUtils.isBlank(weixin)){
            return CodeContent.getModelByCode(0,"微信号不能为空", model);
        }
        try {
            weixin = URLDecoder.decode(weixin, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            return CodeContent.getModelByCode(0,"解码失败", model);
        }

        if (weixin.trim().length() > 20 || weixin.trim().length() <6) {
            return CodeContent.getModelByCode(0,"请输入有效的微信号", model);
        }

        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        user=userService.getById(user.getId());
        //修改用户微信
        user.setWeixin(weixin.trim());
        userService.updateUser(user,token);
        return CodeContent.getModelByCode(1,"sucess", model);

    }
    @ResponseBody
    @RequestMapping(value="/updateMobile.json",produces="application/json;charset=UTF-8")
    public String updateMobile(String companyCode,ModelMap model,String token, String mobile,HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        if (!com.stlm2.util.StringUtils.isMobilePhone(mobile)&&!RegularMatchUtils.isEmail(mobile.trim())) {
            model.put("code",0);
            model.put("msg","请填写正确的格式");
            return new org.json.JSONObject(model).toString();
        }
        if (org.apache.commons.lang.StringUtils.isBlank(mobile)){
            return CodeContent.getModelByCode(0,"手机号不能为空", model);
        }
        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        User user1=userService.getByName(mobile);
        if(user1!=null){
            return CodeContent.getModelByCode(0,"该用户已经存在", model);
        }
        user=userService.getById(user.getId());
        //修改用户手机号
        user.setName(mobile);
        userService.updateUser(user,token);
        return CodeContent.getModelByCode(1,"sucess", model);

    }

    @ResponseBody
    @RequestMapping(value="/updateIdentity.json",produces="application/json;charset=UTF-8")
    public String updateIdentity(String companyCode,ModelMap model,String token, String identity,HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");

        if (org.apache.commons.lang.StringUtils.isBlank(identity)){
            return CodeContent.getModelByCode(0,"身份证号不能为空", model);
        }
        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        user=userService.getById(user.getId());
        //修改用户身份证号
        user.setIdentity(identity.trim());
        userService.updateUser(user,token);
        return CodeContent.getModelByCode(1,"sucess", model);

    }

    @ResponseBody
    @RequestMapping(value="/info.json",produces="application/json;charset=UTF-8")
    public String info(String companyCode,ModelMap model,String token, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");

        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        return CodeContent.getModelByCode(1,"sucess", userService.buildModel(user));

    }

    @ResponseBody
    @RequestMapping(value="/updateAvtor.json",produces="application/json;charset=UTF-8")
    public String updateAvtor(String companyCode,ModelMap model,String token, String avtor,HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");

        if (org.apache.commons.lang.StringUtils.isBlank(avtor)){
            return CodeContent.getModelByCode(0,"头像号不能为空", model);
        }
        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        user=userService.getById(user.getId());
        //修改用户头像
        user.setAvotorr(avtor);
        userService.updateUser(user,token);
        return CodeContent.getModelByCode(1,"sucess", null);

    }

    @ResponseBody
    @RequestMapping(value="/updateQQ.json",produces="application/json;charset=UTF-8")
    public String updateQQ(String companyCode,ModelMap model,String token, String qq,HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");

        if (org.apache.commons.lang.StringUtils.isBlank(qq)){
            return CodeContent.getModelByCode(0,"QQ号不能为空", model);
        }
        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        user=userService.getById(user.getId());
        //修改用户qq号
        user.setQQ(qq);
        userService.updateUser(user,token);
        return CodeContent.getModelByCode(1,"sucess", model);

    }
    @ResponseBody
    @RequestMapping(value="/getAddress.json",produces="application/json;charset=UTF-8")
    public String userAddress(String companyCode,ModelMap model,String token, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");

        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        Address address=addressService.getByUserId(user.getId());
        if(address!=null) {
            model.put("address", addressService.buildModel(address));
            return CodeContent.getModelByCode(1,"sucess", model);

        }
        return CodeContent.getModelByCode(0,"请添加地址",model) ;
    }






    /*
    *ZLQ
    * 个人中心中获取页面头部信息
    * token：用户token
     */
    @ResponseBody
    @RequestMapping(value="/getUserHeadData.json",produces="application/json;charset=UTF-8")
    public String getUserHeadData (String companyCode,ModelMap model,String token){
        String data=null;
            data=userService.getUserHeadData(model, token);


        return data;
    }

    @ResponseBody
    @RequestMapping(value="/getIntegralUserHeadData.json",produces="application/json;charset=UTF-8")
    public String getIntegralUserHeadData (String companyCode,ModelMap model,String token){
        String data=null;
        data=userService.getIntegralUserHeadData(model, token);
        return data;
    }
    @ResponseBody
    @RequestMapping(value="/getIntegralHeadData.json",produces="application/json;charset=UTF-8")
    public String getIntegralHeadData (String companyCode,ModelMap model,String token){
        User user=userService.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();
        Account account=accountService.getByUserId(user.getId());
        if(account==null)
            return CodeContent.returnFailure("没有找到帐户！");
        model.put("integral",account.getRemainJiFen());

        return CodeContent.returnSuccess(model);
    }
    /**
     * ffl
     * 获取用户最近访问的三个城市
     * 2017-06-15 17:07:54
     * @param model
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getUserAddressHistory.json",produces="application/json;charset=UTF-8")
    public String getUserAddressHistory (String companyCode,ModelMap model,String token){
        String data=null;
        return data;
    }

    /**
     * ffl
     * 添加或修改用户访问城市
     * 2017-06-15 17:12:34
     */
    @ResponseBody
    @RequestMapping(value="/updateAddressHistory.json",produces="application/json;charset=UTF-8")
    public String updateUserAddressHistory (String companyCode,ModelMap model,String token,String city){
        String data=null;
        return data;
    }

    /**
     * ffl
     * 用户退出登陆
     * 2017-06-15 18:14:03
     * @param model
     * @param token
     * @return
     */

    @ResponseBody
    @RequestMapping(value="/userLogout.json",produces="application/json;charset=UTF-8")
    public String userLogout (String companyCode,ModelMap model,String token){
        if(com.stlm2.util.StringUtils.isNullOrEmpty(token)){
            return  CodeContent.getModelByCode(0,"未传登陆标识",null);
        }
        try{//检查用户token，清除缓存中当前token
           userService.userTokenLogoutCheck(token);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"不正常退出登陆",model);
        }
        return CodeContent.getModelByCode(1,"",model);
    }


    /*
*ZLQ
* 检验用户是否存在
* userName：用户手机号
 */
    @ResponseBody
    @RequestMapping(value="/checkUserisEmpty.json",produces="application/json;charset=UTF-8")
    public String checkUserisEmpty (String companyCode,ModelMap model,String userName){
        String data=null;
        try{
            data=userService.checkUserisEmpty(model, userName);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"效验失败",model);
        }

        return data;
    }


    /*
*ZLQ
* 系统自动注册用户
* userName：用户手机号
*/
    @ResponseBody
    @RequestMapping(value="/registerUser.json",produces="application/json;charset=UTF-8")
    public String registerUser (String companyCode,ModelMap model,String userName){
        String data=null;
        try{
            data=userService.registerUser(model,userName,companyCode);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"注册失败",model);
        }

        return data;
    }

    /**
     * ffl
     *  2017-07-07 11:19:54
     *  获取用户还返未查看的积分
     * @param model
     * @param token
     * @return
     */

    @ResponseBody
    @RequestMapping(value="/getUserIntegral.json",produces="application/json;charset=UTF-8")
    public String getUserIntegralFromRedis (String companyCode,ModelMap model,String token){
        //return accountService.getMyRedPacket(token);
       // return userIntegralService.getUserIntegral(token);
        return null;
    }

    /**
     * ffl
     * 2017-07-07 11:23:01
     * 用户查看后，用户积分清零 ，重新累计
     * @param model
     * @param token
     * @return
     */

    @ResponseBody
    @RequestMapping(value="/UpdateUserIntegral.json",produces="application/json;charset=UTF-8")
    public String UserIntegralFromRedis (String companyCode,ModelMap model,String token){
       return accountService.updateMyRedPacket(token);
      //return userIntegralService.updateUserIntegral(token);
    }

    /**
     * Author：fuchunyu
     * Date：2017/8/21
     * Description：查看用户类型
     */
    @ResponseBody
    @RequestMapping(value="/getUserType.json",produces="application/json;charset=UTF-8")
    public String getUserType(String companyCode,String token,ModelMap map){
        User user=userDao.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();
        map.put("userType",user.getUserType().getIntValue());
        return CodeContent.returnSuccess(map);
    }


    /**
     * Author：fuchunyu
     * Date：2017/8/21
     * Description：查看用户ID
     */
    @ResponseBody
    @RequestMapping(value="/getUserId.json",produces="application/json;charset=UTF-8")
    public String getUserId(String companyCode,String token,ModelMap map){
        User user=userDao.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();
        map.put("userId",user.getId());
        return CodeContent.returnSuccess(map);
    }

    /**
     *  添加用户收货地址
     * ffl
     * 2017-10-18 15:17:34
     * @param token
     * @param city 城市
     * @param location 经纬度
     * @param detail 详细地址
     * @param ifDefault 是否默认  1默认 0非默认
     * @param name   收货人姓名
     * @param phone 收货人联系电话
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/addUserAddress.json",produces="application/json;charset=UTF-8")
    public String addUserAddress(String companyCode,String token,String city,String location,String detail,String ifDefault,String name,String phone){
        return addressService.addUserAddress(token, city, location, detail, ifDefault, name, phone);
    }

    /**
     * 编辑用户收货地址
     * ffl
     * 2017-10-18 15:04:38
     * @param token
     * @param city  地址城市
     * @param location 经纬度 坐标
     *  @param detail 详细地址
     * @param ifDefault 是否默认  1默认 0非默认
     * @param name   收货人姓名
     * @param phone 收货人联系电话
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/editUserAddress.json",produces="application/json;charset=UTF-8")
    public String editUserAddress(String companyCode,String token,String addressId,String city,String location,String detail,String ifDefault,String name,String phone){
        return addressService.editUserAddress(token, addressId, city, location, detail, ifDefault, name, phone);
    }

    /**
     * 指定地址为默认地址
     * ffl
     * 2017-10-18 15:25:17
     * @param token
     * @param addressId
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/userAddressToBeDefault.json",produces="application/json;charset=UTF-8")
    public String userAddressToBeDefault(String companyCode,String token, String addressId){
        return  addressService.userAddressToBeDefault(token,addressId);
    }

    /**
     *  删除用户地址
     * ffl
     * 2017-10-18 15:29:21
     * @param token
     * @param addressId
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/deleteUserAddress.json",produces="application/json;charset=UTF-8")
    public String deleteUserAddress(String companyCode,String token,String addressId){
        return  addressService.deleteUserAddress(token,addressId);
    }

    /**
     *获取默认地址
     * ffl
     * 2017-10-18 15:29:44
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getDefault.json",produces="application/json;charset=UTF-8")
    public  String getDefault(String companyCode,String token){
        return addressService.getDefault(token);
    }

    /**
     *  获取用户 所有收货地址
     * ffl
     * 2017-10-18 11:45:53
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getUserAddress.json",produces="application/json;charset=UTF-8")
    public String getUserAddress(String companyCode,String token){
        return addressService.getUserAddress(token);
    }

    /**
     * 根据记录id 获取收货地址
     * ffl
     * 2017-11-08 14:39:26
     * @param token
     * @param addressId
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getByAddressId.json",produces="application/json;charset=UTF-8")
    public String getAddressById( String companyCode,String token,String addressId){
        return addressService.getAddressById(token, addressId);
    }

    /**
     * 获取注册红包
     * ffl
     * 2017-09-12 11:47:04
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getRegisterRedPacket.json",produces="application/json;charset=UTF-8")
    public String getMyRegisterRedPacket(String companyCode,String token){
        return userService.getMyRegisterRedPacket(token);
    }

    /**
     * 领取注册红包
     * ffl
     * 2017-09-12 11:47:37
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/updateRegisterRedPacket.json",produces="application/json;charset=UTF-8")
    public String updateMyRegisterRedPacket(String companyCode,String token){
        return userService.updateMyRegisterRedPacket(token);
    }


    /**
     * 通过token发送验证码
     * @param token ：
     * @param  platform：平台
     */
    @ResponseBody
    @RequestMapping(value="/sendMobileCode_Vtoken.json",produces="application/json;charset=UTF-8")
    public String sendMobileCode_Vtoken(String companyCode,ModelMap model, HttpServletResponse response, String token, String platform) {
        response.setCharacterEncoding("utf-8");
        if (StringUtils.isEmpty(token)) {
            return  CodeContent.getModelByCode(-1, "", model);
        }
        User loginUser = userDao.getByToken(token);
        if (loginUser == null) {
            return  CodeContent.getModelByCode(-1, "", model);
        }
        String  mobile = loginUser.getName();
        //生成手机验证码
        String mobileCode = String.valueOf(Math.random()).substring(2, 8);
        System.out.println("mobileCode:"+mobileCode);
        String smsString = "";
        if ("ios".equals(platform)) {
            smsString = smsService.sendCode(Platform.Ios, null, mobile, mobileCode);
        } else if ("android".equals(platform)) {
            smsString = smsService.sendCode(Platform.Android, null, mobile, mobileCode);
        } else {
            smsString = smsService.sendCode(Platform.Wap, null, mobile, mobileCode);
        }
        if("success".equals(smsString)){
            return CodeContent.getModelByCode(1,"sucess", model);
        }else{
            return CodeContent.getModelByCode(0,smsString, model);
        }
    }
    /**
     * 通过手机号发送验证码
     * @param  mobile：手机号
     * @param  platform：平台
     *
     */
    @ResponseBody
    @RequestMapping(value="/sendMobileCode_ValidateCode.json",produces="application/json;charset=UTF-8")
    public String sendMobileCode_ValidateCode(String companyCode,ModelMap model, HttpServletResponse response, String mobile, String platform,String validateCode) {
        response.setCharacterEncoding("utf-8");
        validateCode=StringUtils.trimAllWhitespace(validateCode);
        String smsString = "";
        if (StringUtils.isEmpty(mobile)) {
            return  CodeContent.getModelByCode(0, "请输入手机号", model);
        }
        JedisPool pool = null;
        Jedis jedis = null;
        try {
         pool = RedisClient.getPool();
         jedis = pool.getResource();
            StringBuffer key2=new StringBuffer();
            key2.append("ValidateCode:");
            key2.append(mobile + ":");
            key2.append("true:");
            key2.append(validateCode.toLowerCase());
            boolean boo =jedis.exists(key2.toString());
            if(!boo){
                return CodeContent.getModelByCode(0,"图形验证码验证失败", model);
            }
        //生成手机验证码
        String mobileCode = String.valueOf(Math.random()).substring(2, 8);
        System.out.println("mobileCode:"+mobileCode);

        if ("ios".equals(platform)) {
            smsString = smsService.sendCode(Platform.Ios, null, mobile, mobileCode);
        } else if ("android".equals(platform)) {
            smsString = smsService.sendCode(Platform.Android, null, mobile, mobileCode);
        } else {
            smsString = smsService.sendCode(Platform.Wap, null, mobile, mobileCode);
        }
        } catch (Exception e) {
            e.printStackTrace();
            return CodeContent.getModelByCode(0,"发送验证码失败", model);
        }finally {
            pool.returnResource(jedis);
        }
        if("success".equals(smsString)){
            return CodeContent.getModelByCode(1,"sucess", model);
        }else{
            return CodeContent.getModelByCode(0,smsString, model);
        }
    }

    /**
     * 检测手机号是否在商城已注册
     * ffl
     * 2017-09-26 19:03:38
     * @param mobile
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/checkIfNewUser.json",produces="application/json;charset=UTF-8")
    public String ifNewUser(String companyCode,String mobile){
        if(com.stlm2.util.StringUtils.isNullOrEmpty(mobile)){
            return  CodeContent.getModelByCode(0,"请输入手机号",null);
        }
        User user=userService.getByName(mobile);
        if(user!=null){
            return CodeContent.getModelByCode(-1,"",null);
        }
        return CodeContent.getModelByCode(1,"",null);
    }

    /**
     *根据用户id返回用户姓名
     * ffl
     * 2017-09-26 19:04:36
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/userMSG.json",produces="application/json;charset=UTF-8")
    public String userMSG(String companyCode,String id){
        if(!NumberUtils.isDigits(id)){
            return  CodeContent.getModelByCode(0,"请输入用户id",null);
        }
        User user=userService.getById(NumberUtils.toInt(id));
        if(user==null){
            return CodeContent.getModelByCode(0,"用户不存在",null);
        }
        ModelMap model =new ModelMap();
        model.put("realName",user.getRealName());
        return CodeContent.getModelByCode(1,"",model);
    }


    /**
     * 是否商城注册用户
     * ffl
     * 2017-09-29 19:56:37
     * @param mobile
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/ifRegisterUser.json",produces="application/json;charset=UTF-8")
    public String ifOldUser(String companyCode,String mobile){
        if(com.stlm2.util.StringUtils.isNullOrEmpty(mobile)){
            return  CodeContent.getModelByCode(0,"请输入手机号",null);
        }
        User user=userService.getByName(mobile);
        Boolean registerUser=false;
        if(user!=null){
            registerUser=true;
        }
        ModelMap model=new ModelMap();
        model.put("registerUser",registerUser);
        return CodeContent.getModelByCode(1,"",model);
    }

    @ResponseBody
    @RequestMapping(value="/updateBirthday.json",produces="application/json;charset=UTF-8")
    public String updateBirthday(String companyCode,ModelMap model,String token, String birthday,HttpServletResponse response) throws ParseException {
        response.setCharacterEncoding("UTF-8");

        if(StringUtils.isEmpty(birthday))
            return CodeContent.returnFailure("请输入生日！");
        User user = userService.getByToken(token);
        if (user == null) {
            return CodeContent.getModelByCode(-1,"您的登录已经过期，请重新登录", model);
        }
        user=userService.getById(user.getId());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date=formatter.parse(birthday);
        //修改用户性别
        user.setBirthday(date);
        userService.updateUser(user,token);

        return CodeContent.returnSuccess(null);

    }


    /**
     * 叮当网  用户注册
     * ffl
     * 2017-11-07 15:24:05
     * @param id  邀请码
     * @param mobile
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/registerDDm.json",produces="application/json;charset=UTF-8")
    public String registerDDM (String companyCode,String id,String mobile ,String password){
        return userService.registerDDM(id, mobile, password,companyCode);
    }

    /**
     * 检验短信验证码 并注册
     * ffl
     * 时间
     * @param model
     * @param request
     * @param response
     * @param mobile
     * @param mobile_code
     * @param id
     * @param platform
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/checkCode_register.json",produces="application/json;charset=UTF-8")
    public String appCheckMobileCode_v2(String companyCode,ModelMap model,HttpServletRequest request,HttpServletResponse response, String mobile, String mobile_code, String id,String platform,String password) {
        response.setCharacterEncoding("utf-8");

        if (org.apache.commons.lang.StringUtils.isBlank(mobile)) {
            return CodeContent.getModelByCode(0,"请输入手机号", null);
        }
        if (org.apache.commons.lang.StringUtils.isBlank(mobile_code)) {

            return CodeContent.getModelByCode(0,"请输入验证码", null);
        }
        if(org.apache.commons.lang.StringUtils.isBlank(platform)){
            return CodeContent.getModelByCode(0,"请输入平台", null);
        }
        if(org.apache.commons.lang.StringUtils.isBlank(password)){
            return CodeContent.getModelByCode(0,"请输密码", null);
        }
        if(org.apache.commons.lang.StringUtils.isBlank(id)){
            id="1";
        }
        String result = "";

        if("ios".equals(platform)){
            result = smsService.checkCode(Platform.Ios, mobile, mobile_code,memcached.getCache());

        }else if("android".equals(platform)){

            result = smsService.checkCode(Platform.Android, mobile, mobile_code,memcached.getCache());
        }
        else if("wap".equals(platform)){
            result = smsService.checkCode(Platform.Wap, mobile, mobile_code,memcached.getCache());

        }else if("web".equals(platform)){
            result = smsService.checkCode(Platform.Web, mobile, mobile_code,memcached.getCache());
        }
        if("success".equals(result)){
          return userService.registerDDM(id,mobile,password,companyCode);

        } else{
            return CodeContent.getModelByCode(0,result, null);
        }

    }

}
